﻿using demo86.Models;
using HtmlAgilityPack;
using System;
using System.Collections.Generic;
using System.Data.Entity.Migrations;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Mvc;
using System.Web.UI;

namespace demo86.Controllers
{

    public class ResultController : Controller
    {

        HtmlWeb htmlWeb = new HtmlWeb();

        List<Result> list = new List<Result>();
        // GET: Result
        public ResultDbContext db = new ResultDbContext();
        //public AccountContext dｂ1 = new AccountContext();
        public ActionResult Details(int id)
        {
            Result result = db.Results.Find(id);
            return View(result);
        }
        public ActionResult List(string sortOrder, string Search, string currentFilter, int? page)
        {
            //判断输入值是否为空，为空则直接赋值""，列出的就是所有结果
            //在输入值不为空时不再需要列出所有值,增加运行效率
            var results = from u in db.Results
                          where (u.title.Contains(Search ?? ""))
                          orderby u.title
                          select u;
            //if (!string.IsNullOrEmpty(Search))
            //{
            //    results = results.OrderBy(u => u.title.Contains(Search));
            //}
            //results = results.OrderBy(u => u.title);

            int pageSize = 8;
            int pageNumber = (page ?? 1);
            return View(/*results.ToPagedList(pageNumber, pageSize)*/);
        }

        [HttpGet]
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        //public ActionResult Index(List<Result> getList, string Search, string currentFilter,int ?page,string ex)
        public ActionResult Index(string Search)
        {


            #region 师大
            if (Search != null && (Search.Contains("师大") || Search.Contains("师范") || Search.Contains("www.fjnu.edu") || Search.Contains("yaokuang")))
            {
                List<string> sd = new List<string>() { "13", "19", "18", "90", "101", "98" };
                ViewBag.s1 = "2";
                foreach (var item in sd)
                {
                    string webs = "https://www.fjnu.edu.cn/" + item + "/list.htm";
                    HtmlDocument htmlDoc = htmlWeb.Load(webs);
                    htmlDoc.DocumentNode.SelectNodes("//div[@id='wp_news_w4']/table/*/tr[@class='firstRow']").
                         AsParallel().ToList().ForEach(al =>
                         {
                             string node = al.SelectSingleNode(".//td[@align='left']/a").Attributes["href"].Value;
                             node = node.Contains("http") == false ? "https://www.fjnu.edu.cn" + node : node;
                             HtmlDocument htmlDoc1 = htmlWeb.Load(node);

                             try
                             {
                                 list.Add(new Result
                                 {
                                     // img = "https://www.fjnu.edu.cn"+ htmlDoc.DocumentNode.SelectSingleNode("//div[@class='Article_Content']/p[@style='text-align:center']/img").Attributes["src"].Value,
                                     url = node,
                                     title = al.SelectSingleNode(".//td[@align='left']/a").InnerText,
                                     date = DateTime.Parse(al.SelectSingleNode(".//div[@style='white-space:nowrap']").InnerText),
                                     Type = htmlDoc.DocumentNode.SelectSingleNode("//span[@class='Column_Name']").InnerText,
                                     Where = "福建师范大学",
                                     content = htmlDoc1.DocumentNode.SelectSingleNode("//div[@class='Article_Content']").InnerHtml.Replace("/_upload", "https://www.fjnu.edu.cn/_upload"),
                                 });
                             }
                             catch (Exception)
                             {

                             }


                         });
                }

                ViewBag.total = list.Count();



                return View(list);

            }
            #endregion

            #region 师大测试

            if (Search != null && (Search.Contains("33") || Search.Contains("32") || Search.Contains("www.fjnu.edu") || Search.Contains("yaokuang")))
            {
                string main = "https://www.fjnu.edu.cn/13/list.htm";

                string urlnodes = "//div[@id='wp_news_w4']/table/*/tr[@class='firstRow']";
                string web = "https://www.fjnu.edu.cn";
                string nodes = ".//td[@align='left']/a";
                string title = ".//td[@align='left']/a";
                string date = ".//div[@style='white-space:nowrap']";
                string author = "//div[@class='Article_Content']/p[last()-1]";
                string content = "//div[@class='Article_Content']";


                nodetolist(main, urlnodes, nodes, web, title, date, author, content);



                ViewBag.total = list.Count();
                return View(list);
            }
            #endregion
            #region 江夏学院

            if (Search != null && (Search.Contains("江夏") || Search.Contains("夏院") || Search.Contains("fjjxu.edu")))

            {
                ViewBag.s1 = "2";

                List<string> web = new List<string>()
                {
                    "http://www.fjjxu.edu.cn/html/xwzx/xxxw/1.html",
                    "http://www.fjjxu.edu.cn/html/tzgg/1.html",


                };
                foreach (var item in web)
                {
                    string webs = item;
                    HtmlDocument htmlDoc = htmlWeb.Load(webs);
                    try
                    {

                        htmlDoc.DocumentNode.SelectNodes("//div[@class='list_main_content']/ul/li").
                             AsParallel().ToList().ForEach(al =>
                             {
                                 string node = al.SelectSingleNode(".//span[@class='ecms_list_title']/a").Attributes["href"].Value;
                                 node = node.Contains("http") == false ? "http://www.fjjxu.edu.cn" + node : node;
                                 htmlDoc = htmlWeb.Load(node);
                                 string author1 = htmlDoc.DocumentNode.SelectSingleNode("//div[@class='detail_content_display']/div/p[last()-1]/span[last()]").InnerText;
                                 string te = Regex.Match(author1, @"(.*)\（(.*)\）").Groups[2].Value;
                                 author1 = te;
                                 try
                                 {
                                     list.Add(new Result
                                     {
                                         // img= "http://www.fjjxu.edu.cn" + htmlDoc.DocumentNode.SelectSingleNode("//div[@class='detail_content_display']/*/*/*/img").Attributes["src"].Value ,
                                         url = node.Contains("http") == false ? "http://www.fjjxu.edu.cn" + node : node,
                                         title = al.SelectSingleNode(".//span[@class='ecms_list_title']/a").InnerText,
                                         date = DateTime.Parse(al.SelectSingleNode(".//span[@class='ecms_list_time']").InnerText),
                                         Where = "福建江夏学院",
                                         Type = "夏院要闻",
                                         content = htmlDoc.DocumentNode.SelectSingleNode("//div[@class='detail_content_display']/div").InnerHtml.Replace("/attach/", "http://www.fjjxu.edu.cn/attach/"),
                                         author = author1
                                     });
                                 }
                                 catch (Exception)
                                 {
                                     list.Add(new Result
                                     {
                                         url = node.Contains("http") == false ? "http://www.fjjxu.edu.cn" + node : node,
                                         title = al.SelectSingleNode(".//span[@class='ecms_list_title']/a").InnerText,
                                         date = DateTime.Parse(al.SelectSingleNode(".//span[@class='ecms_list_time']").InnerText),
                                         Where = "福建江夏学院",
                                         Type = "夏院要闻&",
                                         content = htmlDoc.DocumentNode.SelectSingleNode("//div[@class='detail_content_display']/div").InnerHtml.Replace("/attach/", "http://www.fjjxu.edu.cn/attach/"),
                                         author = author1
                                     });
                                 }

                             });
                    }
                    catch (Exception)
                    {


                        htmlDoc.DocumentNode.SelectNodes("//div[@class='list_main_content']/ul/li").
                             AsParallel().ToList().ForEach(al =>
                             {
                                 string node = al.SelectSingleNode(".//span[@class='list_title']/a").Attributes["href"].Value;
                                 node = node.Contains("http") == false ? "http://www.fjjxu.edu.cn" + node : node;
                                 HtmlDocument htmlDoc2 = htmlWeb.Load(node);
                                 list.Add(new Result
                                 {
                                     url = node.Contains("http") == false ? "http://www.fjjxu.edu.cn" + node : node,
                                     title = al.SelectSingleNode(".//span[@class='list_title']/a").InnerText,
                                     date = DateTime.Parse(al.SelectSingleNode(".//span[@class='list_time']").InnerText),
                                     Where = "福建江夏学院",
                                     Type = "通知公告",
                                     content = htmlDoc2.DocumentNode.SelectSingleNode("//div[@class='detail_content_display']/div").InnerHtml.Replace("/attach/", "http://www.fjjxu.edu.cn/attach/"),
                                 });
                             });
                    }
                }

                ViewBag.total = list.Count();
                return View(list);
            }
            #endregion
            #region 志诚学院   全部完成

            if (Search != null && (Search.Contains("志诚") || Search.Contains("志诚学院") || Search.Contains("fdzcxy") || Search.Contains("yaokuang")))
            {
                ViewBag.s1 = "2";
                List<string> sd = new List<string>() { "7", "9", "18", "248", "249", "250", "275", "276" };
                foreach (var item in sd)
                {
                    string webs = "http://www.fdzcxy.com/list.asp?BCid=2&SCid=" + item;
                    HtmlDocument htmlDoc = htmlWeb.Load(webs);
                    string html1 = htmlDoc.DocumentNode.InnerHtml;
                    string encodegg = ldy(html1);
                    htmlWeb.OverrideEncoding = Encoding.GetEncoding(encodegg);
                    htmlDoc = htmlWeb.Load(webs);
                    try
                    {
                        htmlDoc.DocumentNode.SelectNodes("//div[@class='mainlist']/ul/li").
                         AsParallel().ToList().ForEach(al =>
                         {
                             string node = al.SelectSingleNode(".//a").Attributes["href"].Value;
                             node = node.Contains("http") != true ? "http://www.fdzcxy.com/" + node : node;
                             HtmlDocument htmlDoc1 = htmlWeb.Load(node);

                             try
                             {
                                 list.Add(new Result
                                 {
                                     url = node.Contains("http") == false ? "http://www.fdzcxy.com/" + node : node,
                                     title = al.SelectSingleNode(".//a").InnerText,
                                     date = DateTime.Parse(al.SelectSingleNode(".//span").InnerText.Replace("(", "").Replace(")", "")),
                                     Where = "福建志诚学院",
                                     Type = htmlDoc.DocumentNode.SelectSingleNode(".//div[@class='classname']").InnerHtml,
                                     content = htmlDoc1.DocumentNode.SelectSingleNode("//div[@class='contentBody']").InnerHtml.Replace("/admin/editor", "http://www.fdzcxy.com/admin/editor"),
                                     img = "http://www.fdzcxy.com" + htmlDoc1.DocumentNode.SelectSingleNode("//img").Attributes["src"].Value
                                 });
                             }
                             catch (Exception)
                             {
                                 list.Add(new Result
                                 {
                                     url = node.Contains("http") == false ? "http://www.fdzcxy.com/" + node : node,
                                     title = al.SelectSingleNode(".//a").InnerText,
                                     date = DateTime.Parse(al.SelectSingleNode(".//span").InnerText.Replace("(", "").Replace(")", "")),
                                     Where = "福建志诚学院",
                                     Type = htmlDoc.DocumentNode.SelectSingleNode(".//div[@class='classname']").InnerHtml,
                                     content = htmlDoc1.DocumentNode.SelectSingleNode("//div[@class='contentBody']").InnerHtml,
                                 });
                             }

                         });
                    }
                    catch (Exception)
                    {


                    }

                }
                ViewBag.total = list.Count();
                return View(list);
            }
            #endregion

            #region 警察学院   over


            if (Search != null && (Search.Contains("警察") || Search.Contains("警院") || Search.Contains("fjpsc.edu") || Search.Contains("yaokuang")))
            {
                ViewBag.s1 = "2";
                List<string> sd = new List<string>() { "/3dc229a0-9481-42ed-b6ff-32da44390138_0.html", "/554e2c4b-8742-4a36-99d5-9c3c2627d8b4_0.html", "/8a1db449-fdf7-4f36-bfad-e39d6f22d005_0.html" };
                foreach (var item in sd)
                {
                    string webs = "http://www.fjpsc.edu.cn" + item;
                    HtmlDocument htmlDoc = htmlWeb.Load(webs);
                    htmlDoc.DocumentNode.SelectNodes("//table[@class='table1']")
                       .
                         AsParallel().ToList().ForEach(al =>
                         {
                             string node = al.SelectSingleNode(".//td/a").Attributes["href"].Value;
                             node = node.Contains("http") != true ? "http://www.fjpsc.edu.cn/" + node : node;
                             htmlDoc = htmlWeb.Load(node);
                             try
                             {
                                 list.Add(new Result
                                 {
                                     url = node.Contains("http") == false ? "http://www.fjpsc.edu.cn/" + node : node,
                                     title = al.SelectSingleNode(".//td/a").InnerText,
                                     date = DateTime.Parse(al.SelectSingleNode(".//td[2]").InnerText),
                                     Where = "福建警察学院",
                                     Type = al.SelectSingleNode("//div[@class='uc_title']/h1").InnerText,
                                     content = htmlDoc.DocumentNode.SelectSingleNode("//div[@class='newslist']").InnerHtml.Replace("/UploadFiles", "http://www.fjpsc.edu.cn/UploadFiles"),
                                     //   content = htmlDoc.DocumentNode.SelectSingleNode("//div[@class='newslist']/*/tr[3]").InnerText,  看需求先全
                                 });
                             }
                             catch (Exception)
                             {
                                 list.Add(new Result
                                 {
                                     url = node.Contains("http") == false ? "http://www.fjpsc.edu.cn/" + node : node,
                                     title = al.SelectSingleNode(".//td/a").InnerText,
                                     date = DateTime.Parse(al.SelectSingleNode(".//td[2]").InnerText),
                                     Where = "福建警察学院",
                                     Type = al.SelectSingleNode("//div[@class='uc_title']/h1").InnerText,
                                     content = "这是一个神奇的网址，宝宝获取不了"
                                 });
                             }

                         });
                }


                ViewBag.total = list.Count();
                return View(list);
            }
            #endregion
            #region 华南女子

            if (Search != null && (Search.Contains("华南女子") || Search.Contains("华南") || Search.Contains("女子") || Search.Contains("hnwomen") || Search.Contains("yaokuang")))

            {
                ViewBag.s1 = "2";
                List<string> sd = new List<string>() { "http://www.hnwomen.com.cn/xin/gg", "http://www.hnwomen.com.cn/xin/news/" };
                foreach (var item in sd)
                {

                    HtmlDocument htmlDoc = htmlWeb.Load(item);
                    string html = htmlWeb.Load(item).DocumentNode.InnerHtml;
                    var encode5 = ldy(html);
                    ViewBag.ldy = encode5;
                    htmlWeb.OverrideEncoding = Encoding.GetEncoding(encode5);
                    htmlDoc = htmlWeb.Load(item);

                    htmlDoc.DocumentNode.SelectNodes("//ul[@class='news']/li").
                         AsParallel().ToList().ForEach(al =>
                         {
                             string node = al.SelectSingleNode(".//a[@id='n1']").Attributes["href"].Value;
                             node = node.Contains("http") == false ? "http://www.hnwomen.com.cn" + node : node;
                             HtmlDocument htmlDoc1 = htmlWeb.Load(node);
                             list.Add(new Result
                             {
                                 url = node.Contains("http") == false ? "http://www.hnwomen.com.cn" + node : node,
                                 title = al.SelectSingleNode(".//a[@id='n1']").InnerText,
                                 date = DateTime.Parse(al.SelectSingleNode(".//a[@id='n3']").InnerText),
                                 Where = "福建华南女子学院",
                                 Type = htmlDoc.DocumentNode.SelectSingleNode("//div[@class='tit']").InnerText,
                                 content = htmlDoc1.DocumentNode.SelectSingleNode("//div[@align='center']/div[1]").InnerHtml.Replace("/d/", "http://www.hnwomen.com.cn/d/")
                             });
                         });
                }

                ViewBag.total = list.Count();
                return View(list);
            }
            #endregion
            #region 闽江

            if (Search != null && (Search.Contains("闽院") || Search.Contains("闽江") || Search.Contains("www.mju.edu") || Search.Contains("yaokuang")))
            {
                ViewBag.s1 = "2";
                for (int i = 1; i < 6; i++)
                {
                    string web = "http://www.mju.edu.cn/html/xxyw/" + i.ToString() + ".html";

                    HtmlDocument htmlDoc = htmlWeb.Load(web);
                    try
                    {
                        htmlDoc.DocumentNode.SelectNodes("//div[@class='list_main_content']/ul/li").
                        AsParallel().ToList().ForEach(al =>
                        {
                            string node = al.SelectSingleNode(".//span[@class='list_title']/a").Attributes["href"].Value;
                            node = node.Contains("http") == false ? "http://www.mju.edu.cn" + node : node;
                            htmlDoc = htmlWeb.Load(node);
                            list.Add(new Result
                            {
                                url = node.Contains("http") == false ? "http://www.mju.edu.cn" + node : node,
                                title = al.SelectSingleNode(".//span[@class='list_title']/a").InnerText,
                                date = DateTime.Parse(al.SelectSingleNode(".//span[@class='list_time']").InnerText),
                                Where = "福建闽江",
                                Type = "闽江要闻",
                                content = htmlDoc.DocumentNode.SelectSingleNode("//div[@class='detail_content_display']/div[1]").InnerHtml.Replace("/attach", "http://www.mju.edu.cn/attach")
                            });
                        });
                    }
                    catch (Exception)
                    {

                        throw;
                    }


                }

                ViewBag.total = list.Count();
                return View(list);
            }
            #endregion
            #region 农大
            if (Search != null && (Search == "https://www.fafu.edu.cn" || Search.Contains("农大") || Search.Contains("www.fafu.edu") || Search.Contains("农林") || Search.Contains("yaokuang")))
            {
                ViewBag.s1 = "2";
                List<string> sd = new List<string>() { "76", "68", "73", "57", "58", "59", "61", "80", "62", "65" };
                foreach (var item in sd)
                {
                    string webs = "http://www.fafu.edu.cn/52" + item + "/list.htm";
                    try
                    {
                        HtmlDocument htmlDoc = htmlWeb.Load(webs);
                        htmlDoc.DocumentNode.SelectNodes("//div[@id='wp_news_w3']/table/*/tr").
                             AsParallel().ToList().ForEach(al =>
                             {
                                 string node = al.SelectSingleNode(".//a").Attributes["href"].Value;
                                 //ViewBag.ldy = node;
                                 node = node.Contains("http") == false ? "http://www.fafu.edu.cn" + node : node;
                                 HtmlDocument htmlDoc1 = htmlWeb.Load(node);
                                 string html = htmlDoc1.DocumentNode.InnerHtml;

                                 try
                                 {
                                     list.Add(new Result
                                     {
                                         url = node.Contains("http") == false ? "http://www.fafu.edu.cn" + node : node,
                                         title = al.SelectSingleNode(".//td[@align='left']/a").InnerText,
                                         date = DateTime.Parse(al.SelectSingleNode(".//div[@style='white-space:nowrap']").InnerText),
                                         Where = "福建农林大学",
                                         Type = htmlDoc.DocumentNode.SelectSingleNode("//span[@class='Column_Name']").InnerHtml,
                                         content = htmlDoc1.DocumentNode.SelectSingleNode("//div[@class='Article_Content']").InnerHtml.Replace("/_upload/article", "http://www.fafu.edu.cn/_upload/article"),      //content = Regex.Match(html, "^(wp_articlecontent)/.+(nbsp)$").Value
                                     });
                                 }
                                 catch (Exception)
                                 {
                                     try
                                     {
                                         list.Add(new Result
                                         {
                                             url = node.Contains("http") == false ? "http://www.fafu.edu.cn" + node : node,
                                             title = al.SelectSingleNode(".//td[@align='left']/a").InnerText,
                                             date = DateTime.Parse(al.SelectSingleNode(".//div[@style='white-space:nowrap']").InnerText),
                                             Where = "福建农林大学",
                                             Type = "农大公告",
                                             content = htmlDoc.DocumentNode.SelectSingleNode("//div[@class='page']/following-sibling::div[1]").InnerText.Replace("&nbsp;", "").Trim().Replace("&ldquo", "").Replace("&rdquo", "").Replace("&mdash", "——")
                                         });
                                     }
                                     catch (Exception)
                                     {

                                         list.Add(new Result
                                         {
                                             url = node.Contains("http") == false ? "http://www.fafu.edu.cn" + node : node,
                                             title = al.SelectSingleNode(".//td[@align='left']/a").InnerText,
                                             date = DateTime.Parse(al.SelectSingleNode(".//div[@style='white-space:nowrap']").InnerText),
                                             Where = "福建农林大学",
                                             Type = "农大公告",
                                             content = "童鞋这个网址你没法抓取"
                                         });
                                     }


                                 }

                             });
                    }
                    catch (Exception)
                    {


                    }

                }

                ViewBag.total = list.Count();
                return View(list);
            }
            #endregion
            #region 福大
            if (Search != null && (Search.Contains("福大") || Search.Contains("fzu") || Search.Contains("福州大学") || Search.Contains("yaokuang")))
            {
                Search = "http://www.fzu.edu.cn";
                ViewBag.s1 = "2";
                List<string> sd = new List<string>() { "zhxw", "tpxw", "fdrw", "mtfd", "xydt", "ztxw", "xyyc", "fdyw" };
                foreach (var item in sd)
                {
                    string webs = "http://news.fzu.edu.cn/html/" + item + "/1.html";
                    HtmlDocument htmlDoc = htmlWeb.Load(webs);
                    htmlDoc.DocumentNode.SelectNodes("//div[@class='list_main_content']/*/li").
                         AsParallel().ToList().ForEach(al =>
                         {
                             string node = al.SelectSingleNode(".//a").Attributes["href"].Value;
                             node = node.Contains("http") == false ? "http://news.fzu.edu.cn" + node : node;
                             HtmlDocument htmlDoc1 = htmlWeb.Load(node);
                             ViewBag.ldy = node;
                             try
                             {
                                 list.Add(new Result
                                 {
                                     url = node.Contains("http") == false ? "http://news.fzu.edu.cn" + node : node,
                                     title = al.SelectSingleNode(".//a").InnerText,
                                     Where = "福州大学",
                                     Type = htmlDoc.DocumentNode.SelectSingleNode("//div[@class='location']/a[2]").InnerText,
                                     date = DateTime.Parse(al.SelectSingleNode(".//span").InnerText),
                                     content = htmlDoc1.DocumentNode.SelectSingleNode("//div[@id='news_content_display']").InnerHtml.Replace("/attach", "http://news.fzu.edu.cn/attach"),

                                 });
                             }
                             catch (Exception)
                             {
                                 ViewBag.ldy = "meile";
                             }

                         });
                }

                ViewBag.total = list.Count();
                return View(list);
            }
            #endregion
            #region   协和学院  
            if (Search != null && (Search.Contains("cuc.fjnu.edu") || Search.Contains("协") || Search.Contains("yaokuang")))
            {
                ViewBag.s1 = "2";
                List<string> sd = new List<string>() {
                    "http://cuc.fjnu.edu.cn/newscenter/xhyw/index.html",
                    "http://cuc.fjnu.edu.cn/newscenter/bxdt/index.html",
                    "http://cuc.fjnu.edu.cn/newscenter/bxdt/index.html",
                    "http://cuc.fjnu.edu.cn/newscenter/mtxh/index.html",
                    "http://cuc.fjnu.edu.cn/newscenter/dyjl/index.html",
                    "http://cuc.fjnu.edu.cn/newscenter/bfxy/index.html",
                    "http://cuc.fjnu.edu.cn/newscenter/xhjh/index.html",
                    "http://cuc.fjnu.edu.cn/newscenter/xhwy/index.html",
                   "http://cuc.fjnu.edu.cn/newscenter/sxgd/index.html",
                   "http://cuc.fjnu.edu.cn/newscenter/yxxh/index.html",
                   "http://cuc.fjnu.edu.cn/newscenter/ztbd/index.html"};
                foreach (var item in sd)
                {
                    string webs = item;
                    HtmlDocument htmlDo1 = htmlWeb.Load(webs);
                    string htmlyw = htmlDo1.DocumentNode.InnerText;
                    string encodeyw = ldy(htmlyw);
                    htmlWeb.OverrideEncoding = Encoding.GetEncoding(encodeyw);
                    htmlDo1 = htmlWeb.Load(webs);
                    htmlDo1.DocumentNode.SelectNodes("//table[@id='nav2_7Tabcontent_10']").
                      AsParallel().ToList().ForEach(al =>
                      {
                          try
                          {
                              string node = al.SelectSingleNode(".//a").Attributes["href"].Value;
                              node = node.Contains("http") == false ? "http://cuc.fjnu.edu.cn" + node : node;
                              HtmlDocument htmlDoc = htmlWeb.Load(node);
                              string html = htmlDoc.DocumentNode.InnerHtml;
                              string encode = ldy(html);
                              htmlWeb.OverrideEncoding = Encoding.GetEncoding(encode);
                              htmlDoc = htmlWeb.Load(node);
                              try
                              {
                                  list.Add(new Result
                                  {
                                      url = node,
                                      title = al.SelectSingleNode(".//a").InnerText,
                                      Where = "协和学院",
                                      Type = htmlDo1.DocumentNode.SelectSingleNode("//html/body/center/table[2]/tbody/tr/td[3]/div/a").InnerText,
                                      date = DateTime.Parse(al.SelectSingleNode(".//td[last()]").InnerText.Replace("[", "").Replace("]", "")),
                                      content = htmlDoc.DocumentNode.SelectSingleNode("//table[@style='MARGIN-LEFT: 15px']/*/*/td[@class='con14']").InnerHtml.Replace("/newscenter/upload", "http://cuc.fjnu.edu.cn/newscenter/upload")
                                  });
                              }
                              catch (Exception)
                              {


                              }
                          }
                          catch (Exception)
                          {

                              throw;
                          }


                      });
                }

                ViewBag.total = list.Count();
                return View(list);
            }
            #endregion


            return View();
        }

        /// <summary>
        /// 保存到数据库
        /// </summary>
        /// <param name="list">Result对象</param>
        public void SaveToDatabase(List<Result> list)
        {
            //!!!取不出的值请直接给个初始值!!! ""或者0
            foreach (var temp in list)
            {
                Result results = new Result
                {
                    url = temp.url,
                    title = temp.title,
                    img = temp.img,
                    content = temp.content,
                    date = temp.date,
                    author = temp.author,
                    Type = temp.Type,
                    Where = temp.Where
                };
                //var pd = db.Results.Where(u => u.title == temp.title);
                //if (db.Results.Where(u => u.title == temp.title).Count() > 0)
                //{
                //    //若该条目已存在
                //}
                //else
                //{
                //    //不存在则存入
                //    db.Results.AddOrUpdate(results);
                //    db.SaveChanges();
                //}
            }
        }

        /// <summary>
        /// 分页 过滤器
        /// </summary>
        /// <param name="sortOrder">???</param>
        /// <param name="Search">搜索值</param>
        /// <param name="currentFilter">???</param>
        /// <param name="page">???</param>
        /// <returns></returns>
        public ActionResult Show(string sortOrder, string Search, string currentFilter, int? page)
        {
            if (Search != null)
            {
                page = 1;
            }
            else
            {
                Search = currentFilter;

            }
            ViewBag.CurrentFilter = Search;
            var users = from u in db.Results
                        select u;
            if (!string.IsNullOrEmpty(Search))
            {
                users = users.Where(u => u.title.Contains(Search));
            }
            users = users.OrderByDescending(u => u.date);
            int pageSize = 8;
            int pageNumber = (page ?? 1);


            return View(/*users.ToPagedList(pageNumber, pageSize)*/);
        }

        /// <summary>
        /// 获取输入页编码
        /// </summary>
        /// <param name="url">链接</param>
        /// <returns></returns>
        public string GetEncode(string url)
        {
            Uri uri = new Uri(url);
            WebClient webClient = new WebClient();
            string temp = Encoding.GetEncoding("gb2312").GetString(webClient.DownloadData(uri));
            //匹配编码
            Match charset = Regex.Match(temp, "<meta[\\s\\S]*charset=[\\s\\S]*?([-a-zA-Z0-9]+)[\\s\\S]*?>");
            //判断是否获取到编码
            if (string.IsNullOrEmpty(charset.Groups[1].Value))
                return isLuan(temp) ? "gb2312" : "utf-8";
            else
                return charset.Groups[1].Value;
        }

        /// <summary>
        /// 判断是否存在乱码
        /// </summary>
        /// <param name="txt">页面源码</param>
        /// <returns></returns>
        bool isLuan(string txt)
        {
            var bytes = Encoding.UTF8.GetBytes(txt);
            //239 191 189
            for (var i = 0; i < bytes.Length; i++)
            {
                if (i < bytes.Length - 3)
                    if (bytes[i] == 239 && bytes[i + 1] == 191 && bytes[i + 2] == 189)
                    {
                        return true;
                    }
            }
            return false;
        }
        public string ldy(string txt)
        {

            var bytes = Encoding.UTF8.GetBytes(txt);
            //239 191 189
            for (var i = 0; i < bytes.Length; i++)
            {

                if (i < bytes.Length - 3)
                    if (bytes[i] == 239 && bytes[i + 1] == 191 && bytes[i + 2] == 189)
                    {

                        ViewBag.cz = "utf-8".ToString();
                    }
                    else
                    {
                        ViewBag.cz = "gb2312".ToString();
                    }
            }
            return ViewBag.cz;

        }

        public ActionResult OpenLink()
        {
            return Redirect(RouteData.Values["id"].ToString());
        }
        public IEnumerable<Result> nodetolist(string main, string urlnodes, string nodes, string web, string titlenode, string date, string author, string content)
        {
            ViewBag.s1 = "2";
            HtmlDocument htmlDoc = htmlWeb.Load(main);
            htmlDoc.DocumentNode.SelectNodes(urlnodes).
                 AsParallel().ToList().ForEach(al =>
                 {

                     string node = al.SelectSingleNode(nodes).Attributes["href"].Value;
                     node = node.Contains("http") == false ? web + node : node;
                     htmlDoc = htmlWeb.Load(node);
                     list.Add(new Result
                     {
                         url = node,
                         title = al.SelectSingleNode(titlenode).InnerText,
                         date = DateTime.Parse(al.SelectSingleNode(date).InnerText),
                         Where = "福建师范大学",
                         Type = "师大要闻",
                         author = htmlDoc.DocumentNode.SelectSingleNode(author).InnerText.Replace("&nbsp;", "").Trim(),
                         content = htmlDoc.DocumentNode.SelectSingleNode(content).InnerText.Replace("&nbsp;", ""),
                     });
                 });
            ViewBag.total = list.Count();
            return list;

        }
    }
}